from django.shortcuts import render, redirect
# 导包
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
# 导入类视图
from django.views import View

from myapp.models import User, Goods
import json
from django.core.serializers import serialize
from rest_framework.response import Response
from rest_framework.views import APIView
# 导入加密库
import hashlib
# 导入图片库
# 绘画库
from PIL import ImageDraw
# 字体库
from PIL import ImageFont
# 图片库
from PIL import Image
# 随机库
import random
# 文件流
import io

import requests

# 导入上传文件夹配置
from mydjango.settings import UPLOAD_ROOT
import os

# 导入原生sql模块
from django.db import connection

import jwt

# 导入redis数据库
import redis

# 导入时间模块
import time

# 导入公共目录变量
from mydjango.settings import BASE_DIR

# 导包
from django.db.models import Q, F

# 导入dwebsocket的库
# from dwebsocket.decorators import accept_websocket
# import uuid

# 导入序列化对象
from myapp.myser import *

# 定义地址和端口
host = '127.0.0.1'
port = 6379

# 建立redis连接
r = redis.Redis(host=host, port=port)

#商品分类接口
class AddCate(APIView):
    #展示
    def get(self,request):

        cid = request.GET.get('cid')
        #判断
        if cid:
            cates = Category.objects.filter(id=cid).first()
            print(cates)
            return Response({'data': cates.cname})
        else:
            # 查找所有
            cates = Category.objects.all()
            # 序列化
            ser = CategorySer(cates, many=True).data
            return Response({'message': ser})

    #添加分类
    def post(self,request):
        res = {}
        #反序列化操作
        ser = CategorySer(data=request.data)

        #验证
        if ser.is_valid():
            #入库
            ser.save()
            res['code'] = 200
            res['message'] = '添加成功'
            return Response(res)

    #删除
    def delete(self,reqeust):
        res = {}
        #获取参数
        cid = reqeust.query_params.get('cid')
        print(cid)

        #查找分类 并删除
        Category.objects.get(id=cid).delete()
        return  Response({'message':'删除成功','code':200})

    #修改
    def put(self,request):
        cid = request.data.get('cid')
        print(cid)
        #查找分类
        ocates = Category.objects.get(id=cid)

        #修改
        ser = CategorySer(instance=ocates,data=request.data,partial=True)
        if ser.is_valid():
            #保存
            ser.save()
            return Response({'message':'修改成功'})









# 商品入库接口
class AddGoods(APIView):
    def get(self, request):
        name = request.GET.get('name')
        desc = request.GET.get('desc')
        img = request.GET.get('img')
        video = request.GET.get('video')
        param = request.GET.get('param')
        cid = request.GET.get('cid')
        price = request.GET.get('price')

        print(name)
        print(desc)
        print(img)
        print(video)
        print(param)
        print(cid)
        print(price)


        # 排重
        goods = Goods.objects.filter(name=name).first()

        if goods:
            res = {}
            res['code'] = 405
            res['message'] = '该商品已经存在'
            return Response(res)

        # 进行入库
        goods = Goods(name=name, desc=desc, img=img, video=video, param=param, cid=cid, price=price)
        goods.save()

        res = {}
        res['code'] = 200
        res['message'] = '商品添加成功'

        return Response(res)


# 商品列表接口
class UpdateUser(APIView):
    #序列化用户信息给 前端
    def get(self,request):
        res = {}
        #获取参数
        uid = request.GET.get('uid')
        print(uid)
        #查找用户
        user = User.objects.filter(id=uid).all()
        #序列化
        ser  = UpdateUserSerializerGet(instance=user,many=True)
        # print(ser)  UpdateUserSerializerGet(instance=<QuerySet [<User: User object (4)>]>, many=True):
        #     img = CharField(max_length=200)

        # print(ser.data) [OrderedDict([('img', '11111')])]
        res['data'] = ser.data
        return Response(res)








